﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Input;

namespace chemicall
{
    public class ChooseName : MenuScreen
    {
        GameManager gM;
        KeyboardState oldKeyboardState, currentKeyboardState;
        String textString;
        String sauvegarde;

        public ChooseName(String sauvegarde)
            : base("Choisir votre pseudo")
        {
            gM = GameManager.getInstance();
            this.sauvegarde = sauvegarde;
        }

        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (content == null)
                    content = new ContentManager(ScreenManager.Game.Services, "Content");

                _classicFont = content.Load<SpriteFont>("classicPolice");

                this.screenState = ScreenState.Active;

                this.spriteBatch = ScreenManager.SpriteBatch;
            }
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            UpdateInput();
        }

        public void UpdateInput()
        {
            oldKeyboardState = currentKeyboardState;

            currentKeyboardState = Keyboard.GetState();
            Keys[] pressedKeys;
            pressedKeys = currentKeyboardState.GetPressedKeys();

            foreach (Keys key in pressedKeys)
            {
                if (oldKeyboardState.IsKeyUp(key))
                {
                    if (key == Keys.Back && textString.Length>0) // overflows
                        textString = textString.Remove(textString.Length - 1, 1);
                    else
                        if (key == Keys.Space)
                            textString = textString.Insert(textString.Length, " ");
                        else
                            if (key == Keys.Enter)
                            {
                                gM.SaveGameManager(sauvegarde, textString);
                                gM.LoadGameManager(sauvegarde);
                                this.ScreenManager.LastScreen = this;
                                ScreenManager.AddScreen(new MainMenuLoaded(), PlayerIndex.One);
                            }
                            else
                                textString += key.ToString();

                }

            }
        }

        public override void Draw(GameTime gameTime)
        {
            Texture2D backgroundTexture = content.Load<Texture2D>("menuBackground");
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
            
            this.spriteBatch.Begin();
            this.spriteBatch.Draw(backgroundTexture, fullscreen, Color.White);
            this.spriteBatch.DrawString(_classicFont, "Veuillez choisir votre pseudo", new Vector2(225, 100), Color.Blue);
            this.spriteBatch.DrawString(_classicFont, "" + textString, new Vector2(350, 150), Color.White);
            this.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
